import 'package:app_30/models/gogngao_entity.dart';
import 'package:app_30/models/load_entity.dart';
import 'package:app_30/models/regist_num_entity.dart';
import 'package:app_30/models/tgjf_entity.dart';
import 'package:app_30/models/vip_entity.dart';
import 'package:app_30/net/dio_utils.dart';
import 'package:app_30/net/http_api.dart';
import 'package:app_30/res/constant.dart';
import 'package:app_30/util/cache_utils.dart';
import 'package:flutter/cupertino.dart';

class ShareViewModel extends ChangeNotifier {
  GonggaoEntity? gongGao;
  LoadEntity? loadData;
  VipEntity? vipData;

  String title = 'SHARE';
  String tgnr = '';
  String tgjifen = '0';
  String tgfennum = '0';
  String tjnum = '0';
  String tjrnum = '0';

  void initialise() {
    gongGao = getGonggao();
    loadData = getLoadData();
    vipData = getVipData();
    tgfennum = vipData!.tgfennum!;
    tjrnum = vipData!.tjrnum!;
    tgnr = Uri.decodeQueryComponent(gongGao!.tgnr!);
    tgnr = tgnr.replaceAll("[user]", getUser()!);
    notifyListeners();

    searchTgjf();
    searchRegistnum();
    getdwzurl();
  }

  searchTgjf() async {
    Map<String, dynamic> map = Map();
    map['user'] = "${loadData?.user}";
    map['mysqlid'] = "${loadData?.id}";
    map['type'] = Constant.tgjifen;
    map['num'] = "";

    await DioUtils.instance.requestNetwork<TgjfEntity>(
      Method.post,
      HttpApi.chaxun,
      params: map,
      onSuccess: (data) {
        tgjifen = data!.tgjifen!;
        notifyListeners();
      },
      onError: (_, __) {
        print('$_, $__');
      },
    );
  }

  searchRegistnum() async {
    Map<String, dynamic> map = Map();
    map['user'] = "${loadData?.user}";
    map['mysqlid'] = "${loadData?.id}";
    map['type'] = Constant.tjnum;
    map['num'] = "";

    await DioUtils.instance.requestNetwork<RegistNumEntity>(
      Method.post,
      HttpApi.chaxun,
      params: map,
      onSuccess: (data) {
        tjnum = data!.tjnum!;
        notifyListeners();
      },
      onError: (_, __) {
        print('$_, $__');
      },
    );
  }

  getdwzurl() async {
    Map<String, dynamic> map = Map();
    map['user'] = "${loadData?.user}";
    map['url'] = "";
    map['time'] = "";

    await DioUtils.instance.requestNetwork<String>(
      Method.post,
      HttpApi.getdwzurl,
      params: map,
      onSuccess: (data) {
        setTgUrl(Constant.url);
        tgnr = tgnr.replaceAll("[user]", getUser()!);
        tgnr = tgnr.replaceAll("[tgurl]", Constant.url);
        notifyListeners();
      },
      onError: (_, __) {
        print('$_, $__');
      },
    );
  }
}
